/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package biofilter.pipelines;

import biofilter.filters.CompositeFilter;
import biofilter.sources.RecordsFile;
import biofilter.filters.HistogramFilter;
import biofilter.filters.RecordsFile2BowtieRecord;
import biofilter.filters.RecordsFile2MappedPosition;
import biofilter.util.SystemProperties;
import biofilter.records.*;
import biofilter.util.ArrayUtil;
import biofilter.util.ProcessUtil;
import java.io.File;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The Mapper454HistoPipeline takes raw mapped reads from the 454 software and 
 * and it creates a histogram.
 * reads.
 * @author Daniel Quest
 */
public class Mapper454HistoPipeline  extends PipelineBase {

    private RecordsFile fnrf = null;
    private HistogramFilter histoF = null;
    private RecordsFile2MappedPosition rf2mp = null;
    private ProcessUtil pu = new ProcessUtil();
    private SystemProperties sp = new SystemProperties();
    public Mapper454HistoPipeline (String fileNameRecordsFile) throws Exception {
            super();
            this.fnrf = new RecordsFile(fileNameRecordsFile,
                                               "biofilter.records.FileNameRecord");
            Logger.getLogger(Mapper454HistoPipeline.class.getName()).log(Level.INFO, "Mapped Read File: " + fileNameRecordsFile);
            init();
    }

    private void init(){
        try {
            this.filters.setSource(this.fnrf);
            rf2mp = new RecordsFile2MappedPosition();
            rf2mp.setMappedPositionType("biofilter.records.Mapper454Record");
            rf2mp.setSource(this.fnrf);
            this.filters.addFilter(rf2mp);
            histoF = new HistogramFilter();

            this.filters.addFilter(histoF);
            this.outputDir = pu.getOutputDir(sp.get("tmp"));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Mapper454HistoPipeline.class.getName()).log(Level.SEVERE, "tmp not correctly formatted in sys.properties", ex);
        }
    }
    /**
     * Call requireUnique if reads must have one and only one match in-order to be mapped
     */
    public void requireUnique(){
        histoF.unique();
    }

    private String outputDir = null;
    private String targetDatabase = "";

    public String getOutputDir() {
        return outputDir;
    }

    public void setOutputDir(String outputDir) {
        if (!outputDir.endsWith("/")) outputDir = outputDir + "/";
        this.outputDir = outputDir;
    }

    @Override
    public Vector<Record> execPipeline() throws Exception {
        Vector<Record> resultFiles = new Vector<Record>();
//below is wrong... quick and dirty to satisfy a need
        HistogramRecord hr = new HistogramRecord();
        hr.setMapMax(1000);
        Mapper454Record mr = (Mapper454Record) rf2mp.getRecord();
        while (mr != null){
        //for (int i=0; i< 1712122; i++){
            hr.map(mr.getStart(), mr.getEnd(), mr.getStrand(), 1.0);
            mr = (Mapper454Record) rf2mp.getRecord();
           //System.err.println(mr.get());           
        }
        String outfile = this.outputDir + pu.getUniqueFileName(targetDatabase + ".hist");
        Logger.getLogger( Mapper454HistoPipeline.class.getName() ).log(Level.INFO, "Printing Histogram to: " + outfile);
        hr.toFile(outfile);
        return resultFiles; //problem! this is empty!
    }

    public static void main(String[] args) {
        try {
            String runfile = "/auto/transcriptomics/runfiles/454ETOHStress.run";
            Mapper454HistoPipeline m454hp = new Mapper454HistoPipeline(runfile);
            m454hp.execPipeline();

        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}